var paginationTotal = 10;
$(function() {
//    initData(0); //初始化分页<{$lang.data}>
    clear();
    $("#add").dialog({
        autoOpen:false,
        width:400,
        modal:true,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                if ($("#createForm").valid()) {
                    save();
                }
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        }
    });
    $("#right_confirm").dialog({
        autoOpen:false,
        modal:true,
        height:160,
        show:"slide",
        buttons: {
            '<{$lang.ok}>': function() {
                loading();
                $.ajax({
                    type: "post",
                    async: false,
                    dataType: "json",
                    url: "/user/position/delete",
                    data: "id=" + Uid,
                    success: function(json) {
                        if (json.ask == '1') {
                            initData(0);
                            messags = "<{$lang.delete}><{$lang.success}>！";
                        } else {
                            messags = "<{$lang.delete}><{$lang.fail}>！";
                        }
                        $("#message").html(messags);
                    }
                });
                closeLoading();
                $(this).dialog('close');
            },
            '<{$lang.cancel}>': function() {
                $(this).dialog('close');
            }
        }

    });

    //表单验证
    $("#createForm").validate({
        rules: {
            up_name: {
                required: true
            },
            ud_id: {
                required: true
            },
            up_level: {
                required: true
            }
        },
        errorPlacement:function(error, element) {
            element.parent().children(".errormsg").html(error);
        },
        messages: {
            up_name: "<span><img src=\"/images/no.gif\">必填项!</span>",
            ud_id: "<span><img src=\"/images/no.gif\">必选项!</span>",
            up_level: "<span><img src=\"/images/no.gif\">必选项!</span>"
        }
    });
});

function loadData(page, pageSize) {
    var up_name = $('[name=position_name]').val();
    loading();
    $.ajax({ //这里使用到Jquery的ajax<{$lang.method}>
        type: "POST",
        async: false,
        dataType: "json",
        url: "/user/position/list", //请求的处理url
        data:{
            'page':page,    //分页的页数
            'pageSize':pageSize,
            'up_name':up_name
        },
        success: function(json) {
            var list = '';
            if (json.ask != 1) {
                list = "<td colspan='8' class=''>&nbsp;<{$lang.no_data}></td>";
            } else {
                paginationTotal = json.total;
                $.each(json.result, function(key, val) {
                    list += "<tr>";

                    list += "<td>" + val.up_id + "</td>";
                    list += "<td>" + val.up_name + "</td>";
                    list += "<td>" + val.ud_id + "</td>";
                    list += "<td>" + val.up_level + "</td>";

                    list += "<td><a href=\"javascript:editPosition(" + val.up_id + ",'update');\"><img title='<{$lang.edit}>' src='/images/icon/edit.gif'></a></td>";
                    list += "<td><a href=\"javascript:editRight(" + val.up_id + ",'edit');\"><img title='<{$lang.edit}><{$lang.auth}>' src='/images/icon/authority.png'></a></td>";
                    list += "<td><a href=\"javascript:void(0);\"><img title='<{$lang.export}><{$lang.auth}>' src='/images/icon/export.png'></a></td>";
                    list += "<td><a href=\"javascript:void(0)\" onclick=\"delelteInfo(" + val.up_id + ")\"><img title='<{$lang.delete}>' src='/images/icon/delete.gif'></a></td>";

                    list += "</tr>";
                });
            }
            $("#list").html(list);
            closeLoading();
        }
    });
}

function search() {
    initData(0);
}

function editRight(up_id, act) {
    openIframeDialog('/user/position/rights/id/' + up_id, 680, 550);
}

function editPosition(id) {
    clear();
    if (id) {
        $.ajax({
            type: "POST",
            async: false,
            dataType: "json",
            url: "/user/position/single", //请求的处理url
            data:{'id':id},
            success: function(json) {
                if (json.ask == 1) {
                    var val = json.result;
                    $('[name=up_id]').val(val.up_id);
                    $('[name=up_name]').val(val.up_name);
                    $('[name=ud_id]').val(val.ud_id);
                    $('[name=up_level]').val(val.up_level);
                }
                $("#add").dialog("open");
            }
        });
    } else {
        $("#add").dialog("open");
    }
}

function clear() {
    $('[name=up_id]').val('');
    $('[name=up_name]').val('');
    $('[name=ud_id]').val('');
    $('[name=up_level]').val('');
    $('[name=position_name]').val('');	//必须<{$lang.clear}>，否则会报错
    tip('');
}

function save() {
    vForm = '';
    vForm = $("#createForm").serializeArray();
    $.ajax({
        type: "POST",
        async: false,
        dataType: "json",
        url: "/user/position/edit", //请求的处理url
        data: vForm,
        success: function(json) {
            if (json.ask) {
                alertTip('<{$lang.operation}><{$lang.success}>');
                loadData(paginationCurrentPage, paginationPageSize);
                $("#add").dialog("close");
            } else {
                if (json.error == null)return;
                var html = '';
                $.each(json.error, function(key, val) {
                    html += '<span><img src="/images/no.gif">' + val + '</span>';
                })
                tip(html);
            }
        }
    });

}

//<{$lang.delete}>
function delelteInfo(id) {
    if (id == '' || id == undefined) {
        return false;
    }
    Uid = id;
    $('#right_confirm').dialog('open');
}
//<{$lang.tip}>
function tip(mess) {
    var tips = $("#tips");
    tips.html(mess);
    if (mess != '') {
        tips.show();
    } else {
        tips.hide();
    }
    $(".errormsg").each(function() {
        $(this).html('*');
    });
}
//<{$lang.operation}><{$lang.tip}>
function alertTip(tip) {
    $('<div title="<{$lang.tip}>"><p align="center">' + tip + '</p></div>').dialog({
        modal: true,
        buttons: {
            '<{$lang.ok}>': function() {
                $(this).dialog("close");
            }
        }
    });
}